package hl.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TCard;
import hl.model.TMember;
import hl.model.TStuff;
import hl.model.TVenueInfo;
import hl.pageModel.Card;
import hl.pageModel.Charts;
import hl.pageModel.DataGrid;
import hl.pageModel.River;
import hl.service.CardService;

@Service("cardService")
public class CardServiceImpl implements CardService {

	private BaseDao<TCard> cardDao;

	private BaseDao<TVenueInfo> venueInfoDao;

	private BaseDao<TStuff> stuffDao;

	private BaseDao<TMember> memberDao;

	public BaseDao<TMember> getMemberDao() {
		return memberDao;
	}

	public BaseDao<TStuff> getStuffDao() {
		return stuffDao;
	}

	public BaseDao<TVenueInfo> getVenueInfoDao() {
		return venueInfoDao;
	}

	@Autowired
	public void setMemberDao(BaseDao<TMember> memberDao) {
		this.memberDao = memberDao;
	}

	@Autowired
	public void setStuffDao(BaseDao<TStuff> stuffDao) {
		this.stuffDao = stuffDao;
	}

	@Autowired
	public void setVenueInfoDao(BaseDao<TVenueInfo> venueInfoDao) {
		this.venueInfoDao = venueInfoDao;
	}

	public BaseDao<TCard> getCardDao() {
		return cardDao;
	}

	@Autowired
	public void setCardDao(BaseDao<TCard> cardDao) {
		this.cardDao = cardDao;
	}

	@Override
	public DataGrid getDatagrid(Card card) {
		DataGrid dg = new DataGrid();
		String hql = "from TCard t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(card, hql, params);

		if (card.getOrder() != null) {
			hql += " order by " + card.getSort() + " " + card.getOrder();
		}

		List<TCard> l = cardDao.find(hql, params, card.getPage(), card.getRows());

		int totalHql = cardDao.count(hql, params);

		List<Card> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCard t : l) {
				Card u = new Card();
				BeanUtils.copyProperties(t, u);
				u.setMemberName(t.getTMember().getName());
				u.setPkMember(t.getTMember().getPkMember());
				u.setStuffName(t.getTStuff().getName());
				u.setPkStuff(t.getTStuff().getPkStuff());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Card card, String hql, Map<String, Object> params) {
		/*
		 * if (card.getName() != null && !card.getName().trim().equals("")) { hql += " where t.name like :value1 "; params.put("value1", "%" + card.getName().trim() + "%"); }
		 * 
		 * if (card.getGender() != null && !card.getGender().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.gender like :value2 "; params.put("value2", "%" + card.getGender().trim() + "%"); } if (card.getPkcard() != null && !card.getPkcard().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.pkcard like :value3 "; params.put("value3", "%" + card.getPkcard().toString() + "%"); }
		 */
		return hql;
	}

	@Override
	public void save(Card card) {
		TCard t = new TCard();
		BeanUtils.copyProperties(card, t);
		t.setCdatetime(new Date());
		t.setTStuff(stuffDao.get(TStuff.class, card.getPkStuff()));
		t.setTMember(memberDao.get(TMember.class, card.getPkMember()));
		cardDao.save(t);
	}

	@Override
	public void edit(Card card) {
		TCard t = new TCard();
		BeanUtils.copyProperties(card, t);
		cardDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Card card) {
		TCard t = cardDao.get(TCard.class, card.getPkCard());
		cardDao.delete(t);
	}

	@Override
	public Object getComboBox() {
		List<TCard> l = cardDao.find("from TCard");
		List<Card> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TCard t : l) {
				Card u = new Card();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}

	public Date minADay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, -1);
		date = c.getTime();
		return date;
	}

	@Override
	public Object getCharts() {
		Charts c = new Charts();
		Date today = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		try {
			today = sdf.parse(sdf.format(today));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 月卡M 季卡S 半年卡H 年卡Y 两年卡T
		List<Date> ld = new ArrayList();
		List<Integer> m = new ArrayList();
		List<Integer> s = new ArrayList();
		List<Integer> h = new ArrayList();
		List<Integer> y = new ArrayList();
		List<Integer> t = new ArrayList();
		int m1 = cardDao.count("from TCard t where t.cdatetime> ? and t.cardType=?", new Object[] { today, "月卡" });
		int s1 = cardDao.count("from TCard t where t.cdatetime> ? and t.cardType=?", new Object[] { today, "季卡" });
		int h1 = cardDao.count("from TCard t where t.cdatetime> ? and t.cardType=?", new Object[] { today, "半年卡" });
		int y1 = cardDao.count("from TCard t where t.cdatetime> ? and t.cardType=?", new Object[] { today, "年卡" });
		int t1 = cardDao.count("from TCard t where t.cdatetime> ? and t.cardType=?", new Object[] { today, "两年卡" });
		Date d1 = minADay(today);

		int m2 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d1, today, "月卡" });
		int s2 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d1, today, "季卡" });
		int h2 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d1, today, "半年卡" });
		int y2 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d1, today, "年卡" });
		int t2 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d1, today, "月卡" });
		Date d2 = minADay(d1);

		int m3 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d2, d1, "月卡" });
		int s3 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d2, d1, "季卡" });
		int h3 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d2, d1, "半年卡" });
		int y3 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d2, d1, "年卡" });
		int t3 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d2, d1, "月卡" });
		Date d3 = minADay(d2);

		int m4 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d3, d2, "月卡" });
		int s4 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d3, d2, "季卡" });
		int h4 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d3, d2, "半年卡" });
		int y4 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d3, d2, "年卡" });
		int t4 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d3, d2, "月卡" });
		Date d4 = minADay(d3);

		int m5 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d4, d3, "月卡" });
		int s5 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d4, d3, "季卡" });
		int h5 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d4, d3, "半年卡" });
		int y5 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d4, d3, "年卡" });
		int t5 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d4, d3, "月卡" });
		Date d5 = minADay(d4);

		int m6 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d5, d4, "月卡" });
		int s6 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d5, d4, "季卡" });
		int h6 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d5, d4, "半年卡" });
		int y6 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d5, d4, "年卡" });
		int t6 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d5, d4, "月卡" });
		Date d6 = minADay(d5);

		int m7 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d6, d5, "月卡" });
		int s7 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d6, d5, "季卡" });
		int h7 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d6, d5, "半年卡" });
		int y7 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d6, d5, "年卡" });
		int t7 = cardDao.count("from TCard t where t.cdatetime> ? and t.cdatetime< ? and t.cardType=?", new Object[] { d6, d5, "月卡" });

		ld.add(d6);
		ld.add(d5);
		ld.add(d4);
		ld.add(d3);
		ld.add(d2);
		ld.add(d1);
		ld.add(today);
		m.add(m7);
		m.add(m6);
		m.add(m5);
		m.add(m4);
		m.add(m3);
		m.add(m2);
		m.add(m1);
		s.add(s7);
		s.add(s6);
		s.add(s5);
		s.add(s4);
		s.add(s3);
		s.add(s2);
		s.add(s1);
		h.add(h7);
		h.add(h6);
		h.add(h5);
		h.add(h4);
		h.add(h3);
		h.add(h2);
		h.add(h1);
		y.add(y7);
		y.add(y6);
		y.add(y5);
		y.add(y4);
		y.add(y3);
		y.add(y2);
		y.add(y1);
		t.add(t7);
		t.add(t6);
		t.add(t5);
		t.add(t4);
		t.add(t3);
		t.add(t2);
		t.add(m1);
		List test = new ArrayList();
		List<River> x = new ArrayList();

		for (int j = 0; j < 7; j++) {
			River r = new River();
			r.setDate(sdf.format(ld.get(j)));
			r.setCount(m.get(j));
			r.setClazz("月卡");
			x.add(r);
		}
		for (int j = 0; j < 7; j++) {
			River r = new River();
			r.setDate(sdf.format(ld.get(j)));
			r.setCount(s.get(j));
			r.setClazz("季卡");
			x.add(r);
		}
		for (int j = 0; j < 7; j++) {
			River r = new River();
			r.setDate(sdf.format(ld.get(j)));
			r.setCount(h.get(j));
			r.setClazz("半年卡");
			x.add(r);
		}
		for (int j = 0; j < 7; j++) {
			River r = new River();
			r.setDate(sdf.format(ld.get(j)));
			r.setCount(y.get(j));
			r.setClazz("年卡");
			x.add(r);
		}
		for (int j = 0; j < 7; j++) {
			River r = new River();
			r.setDate(sdf.format(ld.get(j)));
			r.setCount(t.get(j));
			r.setClazz("两年卡");
			x.add(r);
		}
/*
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 5; j++) {
				River r = new River();
				r.setDate(sdf.format(ld.get(i)));
				r.setCount(m.get(j));
				r.setClazz("月卡");
				x.add(r);

				River r1 = new River();
				r1.setDate(sdf.format(ld.get(i)));
				r1.setCount(s.get(j));
				r1.setClazz("季卡");
				x.add(r1);

				River r2 = new River();
				r2.setDate(sdf.format(ld.get(i)));
				r2.setCount(h.get(j));
				r2.setClazz("半年卡");
				x.add(r2);

				River r3 = new River();
				r3.setDate(sdf.format(ld.get(i)));
				r3.setCount(y.get(j));
				r3.setClazz("年卡");
				x.add(r3);

				River r4 = new River();
				r4.setDate(sdf.format(ld.get(i)));
				r4.setCount(t.get(j));
				r4.setClazz("两年卡");
				x.add(r4);
			}
		}
*/
		String json = new String();
		json=json+"[";
		for (River z : x) {
			json = json + "['" + z.getDate() + "'," + z.getCount() + ",'" + z.getClazz() + "'],";
		}
		json = json.substring(0, json.length() - 1);
		json=json+"]";
		c.setDetail5(json);
		return json;
	}

}
